package com.tuanzhang.mall.goods.types.req;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.tuanzhang.mall.seller.types.exception.ApiException;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class SpuSaveParam {

    private Long id;

    /**
     * 用户id
     */
    private Long userId;

    /**
     * 类目id
     */
    private Long categoryId;

    /**
     * 商品编码
     */
    private String spuCode;

    /**
     * 商品名称
     */
    private String spuName;

    /**
     * 商品图片 xx,xxx
     */
    private String spuImages;

    /**
     * 视频 xx,xxx
     */
    private String spuVideo;

    /**
     * 视频英语
     */
    private String spuVideoEn;

    /**
     * 视频阿拉伯语
     */
    private String spuVideoAr;

    /**
     * 视频印尼语
     */
    private String spuVideoIdn;

    /**
     * 品牌id
     */
    private Long brandId;

    /**
     * 品牌授权书
     */
    private String brandLicenseUrl;

    /**
     * 属性 :[{"id":1,"attr":"颜色","items":{"id":1,"value":"黄"}},{"id":1,"attr":"内存","items":{"id":2,"value":"256"}}]
     */

    private List<Attr> attr;

    /**
     * 规格 [{"id":1,"spec":"颜色","items":[{"id":1,"value":"黄","image":""},{"value":"红","image":""}]},{"spec":"内存","items":[{"value":"256"}]}]
     */
    private List<Spec> spec;

    /**
     * 发货地-省code
     */
    private String deliveryProvinceCode;

    /**
     * 发货地-省名称
     */
    private String deliveryProvinceName;

    /**
     * 发货地-市code
     */
    private String deliveryCityCode;

    /**
     * 发货地-市名称
     */
    private String deliveryCityName;

    /**
     * 发货地-区code
     */
    private String deliveryAreaCode;

    /**
     * 发货地-区名称
     */
    private String deliveryAreaName;

    /**
     * 发货时效 0-24小时 1-48小时 2-72小时 3-7天 4-15天
     */
    private Integer deliveryLimit;

    /**
     * 运费类型 0-运费待议 1-运费模板
     */
    private Integer shippingType;

    /**
     * 详情
     */
    private String spuInfo;

    /**
     * 起批数量
     */
    private Integer saleMinNum;

    /**
     * 销售币种 1-人民币，2-美元，3-阿联酋迪拉姆,4-沙特里亚尔
     */
    private Integer priceType;

    /**
     * sku集合
     */
    private List<Sku> skuList;

    @Data
    public static class Sku {
        /**
         * id
         */
        private Long id;

        /**
         * sku编码
         */
        private String skuNo;

        /**
         * sku条码
         */
        private String skuCode;

        /**
         * 黄,256
         */
        private String specValue;

        /**
         * [{"id":1,"spec": "颜色","item": {"id":1,"value":"黄"}},{"id":1,"spec": "尺寸","item": {"value":"xxl"}}]
         */
        private List<Spec> spec;

        /**
         * 价格
         */
        private BigDecimal price;

        /**
         * 库存
         */
        private Integer stock;

        @Data
        public static class Spec {
            @JSONField(serializeUsing = ToStringSerializer.class)
            private Long id;
            private String spec;
            private SpecItem item;

        }

        @Data
        public static class SpecItem {
            @JSONField(serializeUsing = ToStringSerializer.class)
            private Long id;
            private String value;
        }
    }

    @Data
    public static class Attr {
        @JSONField(serializeUsing = ToStringSerializer.class)
        private Long id;
        private String attr;

        private List<AttrItem> items;

    }

    @Data
    public static class AttrItem {
        @JSONField(serializeUsing = ToStringSerializer.class)
        private Long id;
        private String value;
    }


    @Data
    public static class Spec {
        @JSONField(serializeUsing = ToStringSerializer.class)
        private Long id;
        private String spec;
        private List<SpecItem> items;

    }

    @Data
    public static class SpecItem {
        @JSONField(serializeUsing = ToStringSerializer.class)
        private Long id;
        private String image;
        private String value;
    }

    public void valid() {
        List<Spec> spec = getSpec();
        List<String> allSpec = getAllSpec(spec, 0, new ArrayList<>());
        for (Sku sku : getSkuList()) {
            String specValue = sku.getSpec().stream().map(t -> t.getItem().getId()).sorted().map(t -> t.toString()).collect(Collectors.joining(","));
            if (!allSpec.contains(specValue)) {
                throw new ApiException("商品规格异常");
            }
            sku.setSpecValue(specValue);
            allSpec.remove(specValue);
        }
        if (CollectionUtil.isNotEmpty(allSpec)) {
            throw new ApiException("商品规格异常");
        }
    }

    private List<String> getAllSpec(List<Spec> specList, Integer index, List<List<Long>> result) {
        if (specList.size() <= index) {
            return result.stream().map(t -> t.stream().sorted().map(o -> o.toString()).collect(Collectors.joining(","))).collect(Collectors.toList());
        }

        Spec spec = specList.get(index);
        List<List<Long>> newResult = new ArrayList<>();
        for (SpecItem item : spec.getItems()) {
            if (index == 0) {
                List<Long> newList = new ArrayList<>();
                newList.add(item.getId());

                newResult.add(newList);
            } else {
                for (List<Long> list : result) {
                    List<Long> newList = new ArrayList<>(list);
                    newList.add(item.getId());

                    newResult.add(newList);
                }
            }
        }
        return getAllSpec(specList, index + 1, newResult);
    }
}
